<?php
namespace app\admin\controller;

class Admin extends Common
{
    public function admin_list()
    {
        $ret = db('admin')->order("admin_addtime desc")->paginate(5);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',2);
        return view('admin_list');
    }

    pubLic function admin_pass()
    {
        $admin_id = input('get.admin_id');
        $ret = db('admin')->where("admin_id={$admin_id}")->find();
        if($ret['admin_power']== '1'){
            return [
                'msg'    => '已是启用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('admin')->where("admin_id={$admin_id}")->update(['admin_power'=>'1']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }
    pubLic function admin_fail()
    {
        $admin_id = input('get.admin_id');
        $ret = db('admin')->where("admin_id={$admin_id}")->find();
        if($ret['admin_power']== '0')
        {
            return [
                'msg'    => '已是禁用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('admin')->where("admin_id={$admin_id}")->update(['admin_power'=>'0']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }

    pubLic function admin_super_pass()
    {
        $admin_id = input('get.admin_id');
        $ret = db('admin')->where("admin_id={$admin_id}")->find();
        if($ret['admin_super']== '1'){
            return [
                'msg'    => '已是启用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('admin')->where("admin_id={$admin_id}")->update(['admin_super'=>'1']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }
    pubLic function admin_super_fail()
    {
        $admin_id = input('get.admin_id');
        $ret = db('admin')->where("admin_id={$admin_id}")->find();
        if($ret['admin_super']== '0')
        {
            return [
                'msg'    => '已是禁用状态！',
                'status' => 0
            ];
        }
        else
        {
            $ret = db('admin')->where("admin_id={$admin_id}")->update(['admin_super'=>'0']);
            if ( $ret == false )
            {
                return [
                    'msg'    => '更改失败',
                    'status' => 0
                ];
            }
            return [
                'msg'    => '更改成功',
                'status' => 1
            ];
        }
    }

    public function admin_del()
    {
        $admin_id = input('get.admin_id');
        $ret = db('admin')->where("admin_id={$admin_id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function add_admin(){
        $this->assign('pick',2);
        return view('add_admin');
    }

    public function add_admin_action(){
        $admin_name    = input('post.admin_name');
        $admin_account   = input('post.admin_account');
        $admin_pwd    = input('post.admin_pwd');
        $admin_power    = input('post.admin_power');
        $admin_super    = input('post.admin_super');
        $file = request()->file('admin_himg');
        $img_src = "";
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
               $img_src = '/uploads/'.$info->getSaveName();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

        $data = [
            'admin_name'        => $admin_name,
            'admin_account'     => $admin_account,
            'admin_pwd'         => md5($admin_pwd),
            'admin_power'       => $admin_power,
            'admin_super'       => $admin_super,
            'admin_himg'        => $img_src,
            'admin_addtime'     => time()
        ]; 
        
        $ret = db('admin')->insert($data);
        if($ret ==false)
        {
            $this->error("添加管理员失败！");
        }
        $this->success("添加管理员成功！",'/admin/admin/admin_list');
    }

    public function modify_admin(){
        $admin_id = $_GET['admin_id'];
        $admin_info = db('admin')->where("admin_id={$admin_id}")->find();
        $this->assign('pick',2);
        $this->assign('admin_info',$admin_info);
        return view('modify_admin');
    }

    public function modify_admin_action(){
        header("Content-Type:text/html;charset=utf8");
        $admin_id      = input('post.admin_id');
        $admin_name    = input('post.admin_name');
        $admin_account = input('post.admin_account');
        $admin_pwd     = input('post.admin_pwd');
        $admin_real_pwd = db('admin')->where("admin_id = {$admin_id}")->value("admin_pwd");
        if($admin_real_pwd != $admin_pwd){
            $admin_pwd = md5($admin_pwd);
        }
        $file = request()->file('admin_himg');
        $img_src = "";
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
               $img_src = '/uploads/'.$info->getSaveName();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        if(!$img_src)
        {
           $data = [
            'admin_name'        => $admin_name,
            'admin_account'     => $admin_account,
            'admin_pwd'         => $admin_pwd
        ];  
        }
        else
        {
           $data = [
            'admin_name'        => $admin_name,
            'admin_account'     => $admin_account,
            'admin_pwd'         => $admin_pwd,
            'admin_himg'        => $img_src
        ];      
        }
        $ret = db('admin')->where("admin_id={$admin_id}")->update($data);
        if($ret ==false)
        {
            $this->error("更新管理员失败！");
        }
        $this->success("更新管理员成功！",'/admin/admin/admin_list'); 
    }
}
